package com.airlines.crewassignment.service.crew.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.common.Page;
import com.airlines.crewassignment.dao.crew.CrewConflictMapper;
import com.airlines.crewassignment.entity.crew.CrewConflict;
import com.airlines.crewassignment.entity.crew.CrewConflictOther;
import com.airlines.crewassignment.service.crew.CrewConflictService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @author pxy
 * @date 2018/05/17 15:27:03
 * @company xxxx Airlines
 * @description 
 */
@Service
public class CrewConflictServiceImpl implements CrewConflictService {

    @Autowired
    private CrewConflictMapper crewConflictMapper;

    @Override
    public int insert(CrewConflict crewConflict){
        return crewConflictMapper.insert(crewConflict);
    }

    @Override
    public int delete(Integer id){
        return crewConflictMapper.delete(id);
    }


    @Override
    public int update(CrewConflict crewConflict){
        return crewConflictMapper.update(crewConflict);
    }

    @Override
    public CrewConflict get(Integer id){
        return crewConflictMapper.get(id);
    }

    @Override
    public List<CrewConflict> list(CrewConflict crewConflict){
        return crewConflictMapper.list(crewConflict);
    }

    @Override
    public Page list(CrewConflict crewConflict, Page page){
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        List<CrewConflict> list = crewConflictMapper.listByFuzzy(crewConflict);
        PageInfo<CrewConflict> pageInfo = new PageInfo<CrewConflict>(list);
        page.setTotal(String.valueOf(pageInfo.getTotal()));
        page.setRows(pageInfo.getList());
        return page;
    }

    @Override
    public List<CrewConflictOther> listCrewConflictOther() {
        CrewConflict query = new CrewConflict();
        List<CrewConflict> list = crewConflictMapper.listByFuzzy(query);
        Set<String> workcodeSet = new HashSet<>();
        for(CrewConflict c : list){
            workcodeSet.add(c.getWorkcode1());
            workcodeSet.add(c.getWorkcode2());
        }

        List<CrewConflictOther> crewConflictOtherList = new ArrayList<>();
        for(String workcode : workcodeSet){
            CrewConflictOther crewConflictOther = new CrewConflictOther();
            crewConflictOther.setWorkcode(workcode);

            Set<String> conflictWorkcodeSet = new HashSet<>();
            for(CrewConflict c : list){
                if(StringUtils.equals(c.getWorkcode1(), workcode)){
                    conflictWorkcodeSet.add(c.getWorkcode2());
                }
                if(StringUtils.equals(c.getWorkcode2(), workcode)){
                    conflictWorkcodeSet.add(c.getWorkcode1());
                }
            }
            List<String> conflictWorkcodeList = new ArrayList<>(conflictWorkcodeSet);
            crewConflictOther.setConflictWorkcodeList(conflictWorkcodeList);

            crewConflictOtherList.add(crewConflictOther);
        }
        return crewConflictOtherList;
    }



}
